#ifndef VIDEOVIEW_HXX
#define VIDEOVIEW_HXX

#include <QWidget>
#include <QTimer>
#include <QMouseEvent>
#include <QShortCut>
#include <QMainWindow>

#include <vector>

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/core/ocl.hpp>

class Project;
class VideoController;
class TrackedObject;
class ShapeFish;
class MainWindow;

enum ViewStatus
{
	VW_STATUS_DISPLAYING,
	VW_STATUS_EDITING,
	VW_STATUS_FRAME_DRAGING
};

class VideoView : public QWidget
{
    Q_OBJECT
public:
    explicit VideoView(QWidget *parent = nullptr);
    virtual void paintEvent(QPaintEvent *event);

	QRect cvRect2QRect(cv::Rect2d rect);
	cv::Rect2d qRect2cvRect(QRect rect);

	QPoint cvPoint2QPoint(cv::Point2d cpt);
	cv::Point2d qPoint2cvPoint(QPoint qpt);
	cv::Point2d qOffset2cvPoint(QPoint qpt);

	QRect getImageQRect();

	void drawTrackingObject(QPainter &painter);
	void drawTrackingObjectRect(QPainter& painter);
	void drawTrackingObjectFish(QPainter& painter);

	void drawNewObject(QPainter &painter);
	void drawNewObjectRect(QPainter& painter);
	void drawNewObjectFish(QPainter& painter);

	void drawTrackedObject(QPainter &painter);
    void drawTrackedObjectRect(QPainter& painters,TrackedObject* object);
    void drawTrackedObjectFish(QPainter& painter, TrackedObject* object);

	void drawSelectedObjectRect(QPainter& painter, QRect& rect);
	void drawEditingObjectRect(QPainter& painter, QRect& rect);
	void drawSelectedObjectFish(QPainter& painter, ShapeFish* shapeFish);

	void drawTextInfo(QPainter &painter);

	void rects2objects();

	void reset();

signals:
	void frameInfoUpdated();
	void rectAdded();
	void objectSelected();
	void objectUnSelected();

public slots:
    void updateFrame();

	void onKeyEscape();
	void onKeySpace();
	//void onKeyDrag();
	//void onKeyArrow();

	void zoomIn();
	void zoomOut();
	void zoomFit();

public:
    inline void setProject(Project* project) { _project = project; }
	inline Project* getProject() { return _project; }
	inline void setController(VideoController* controller) { _controller = controller; }

	void mousePressEvent(QMouseEvent *event);
	void mouseMoveEvent(QMouseEvent *event);
	void mouseReleaseEvent(QMouseEvent *event);
	void mouseDoubleClickEvent(QMouseEvent *event);
	void keyPressEvent(QKeyEvent* event);

	void wheelEvent(QWheelEvent* event);

	void clearRects() { _rects.clear(); }

	inline std::vector<QRect> getRects() { return _rects; }
	std::vector<cv::Rect2d> getCvRects();

	inline int getCurrentFrameIndex() { return _frameIndex; }
	inline ViewStatus getViewStatus() { return _status; }
	inline void setViewStatus(ViewStatus status) { _status = status; }

	inline void setMainWindow(MainWindow* window) { _mainWindow = window; }

	void pause();
	void resume();

private:
	void drawFishHeadCenter(QPainter& painter, ShapeFish* shape);
	void drawFishMassCenter(QPainter& painter, ShapeFish* shape);

private:
    uchar* _imageData;

    Project* _project;
	VideoController* _controller;

	QTimer* _timer;

	bool _dragingRect;
	QPoint _startPoint;
	QPoint _endPoint;

	std::vector<QRect> _rects;

	int _frameIndex;

	ViewStatus _status;
	ViewStatus _prevStatus;

	float _zoomScale;
	float _zoomAutoScale;
	QPoint _zoomOffset;
	QPoint _zoomTestPos;

	MainWindow* _mainWindow;
};

#endif // VIDEOVIEW_HXX
